﻿using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;

namespace tools.office.npoi
{
    /// <summary>
    /// 单元格样式帮助类
    /// </summary>
    public static class CellStyleHelper
    {
        /// <summary>
        /// 创建单元格样式
        /// </summary>
        /// <param name="workbook">工作簿实例</param>
        /// <param name="font">字体实例</param>
        /// <param name="horizontalAlignment">水平对齐方式</param>
        /// <param name="wrapText">是否自动换行（默认 => false）</param>
        /// <param name="displayBorder">是否显示边框（默认 => false）</param>
        /// <returns>单元格样式实例</returns>
        public static ICellStyle GetCellStyle(
            IWorkbook workbook,
            IFont font,
            HorizontalAlignment horizontalAlignment,
            bool wrapText = false,
            bool displayBorder = false)
        {
            ICellStyle cs = workbook.CreateCellStyle();
            cs.SetFont(font);
            cs.Alignment = horizontalAlignment;
            cs.VerticalAlignment = VerticalAlignment.Center; // 垂直居中
            cs.WrapText = wrapText;

            if (displayBorder)
            {
                cs.BorderTop = BorderStyle.Thin;
                cs.BorderRight = BorderStyle.Thin;
                cs.BorderBottom = BorderStyle.Thin;
                cs.BorderLeft = BorderStyle.Thin;
            }

            return cs;
        }

        /// <summary>
        /// 为当前单元格样式填充前景色
        /// </summary>
        /// <param name="workbook">工作簿实例</param>
        /// <param name="cellStyle">单元格样式实例</param>
        /// <param name="colorRGB">颜色 RGB 值</param>
        /// <returns>新的单元格样式实例</returns>
        public static ICellStyle FillForegroundColor(IWorkbook workbook, ICellStyle cellStyle, byte[] colorRGB)
        {
            ICellStyle cs = workbook.CreateCellStyle();
            cs.CloneStyleFrom(cellStyle);
            cs.FillPattern = FillPattern.SolidForeground;
            ((XSSFCellStyle)cs).SetFillForegroundColor(new XSSFColor(colorRGB));

            return cs;
        }
    }
}
